package com.heima.admin.controller.v1;

import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(value = "频道管理API", tags = "频道管理API")
@RestController//方法返回格式是json格式数据
@RequestMapping("/api/v1/channel")//请求路径
public class AdChannnelController {
    @Autowired
    ChannelService channelService;

    @ApiOperation("频道分页列表模糊查询")
    @PostMapping("list")//post请求方式
    public ResponseResult list(@RequestBody ChannelDto dto, HttpServletRequest request){
        System.out.println(request);

        return channelService.findByNameAndPage(dto);
    }

    @ApiOperation(value = "添加频道",notes = "频道不能为空，不能重复")
    @PostMapping("save")//post请求方式
    public ResponseResult list(@RequestBody AdChannel channel){

        return channelService.insert(channel);
    }

    @ApiOperation(value = "频道修改",notes = "修改和生效")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel adChannel) {
        return channelService.update(adChannel);
    }

    @ApiOperation("频道删除")
    @GetMapping("/del/{id}")
    public ResponseResult delete(@PathVariable Integer id) {
        return channelService.delete(id);
    }

    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        System.out.println("list = " + list);
        return ResponseResult.okResult(list);

    }
}
